package com.ev.common.jobs;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ev.asl.constant.AslConstant;
import com.ev.custom.dao.PatrolPlanDao;
import com.ev.custom.dao.UpkeepPlanDao;
import com.ev.custom.service.*;
import com.ev.framework.config.ApplicationContextRegister;
import com.ev.framework.config.ConstantForDevice;

import com.ev.framework.utils.DateFormatUtil;
import com.ev.common.vo.PlanVo;
import com.ev.custom.domain.PatrolPlanDO;
import com.ev.custom.domain.PatrolRecordDO;
import com.ev.custom.domain.UpkeepRecordDO;
import com.ev.framework.utils.ShiroUtils;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * excuteNotice:根据计划配置的规则定时生成第二天的巡检单和保养单。
 * updatePatrolPlan:根据计划的结束时间更新计划单的状态为已完成状态
 */
@Component
public class NoticeJob {
    private static final Logger log = LoggerFactory.getLogger(NoticeJob.class);

    @Scheduled(cron = "0 30 23 * * ? ")
    public void executeInternal() {
        try {
            //根据计划配置的规则定时生成第二天的巡检单和保养单。
            excuteNotice();
            //根据计划的结束时间更新计划单的状态为已完成状态
            updatePatrolPlan();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void executeInternal2() {
        try {
            //根据计划配置的规则定时生成第二天的巡检单和保养单。
            log.error("================4444444444444:");
            excuteNotice2();
            //根据计划的结束时间更新计划单的状态为已完成状态
            updatePatrolPlan();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    //随机返回字符串数组中的字符串
    public static String RandomStr(String[] strs) {
        int random_index = (int) (Math.random() * strs.length);
        return strs[random_index];
    }


    /**
     * 执行任务方法
     *
     * @throws ParseException
     */
    private void excuteNotice() throws ParseException {
        //巡检计划单
        PatrolPlanService patrolPlanService = ApplicationContextRegister.getBean(PatrolPlanService.class);
        PatrolRecordService patrolRecordService = ApplicationContextRegister.getBean(PatrolRecordService.class);
        UpkeepRecordService upkeepRecordService = ApplicationContextRegister.getBean(UpkeepRecordService.class);
        Date now = new Date();
        String tomrrowStr = new SimpleDateFormat("yyyy-MM-dd").format(DateUtils.addDays(now, 1));
        // 1、查询运行中的计划单
        Map<String, Object> map = new HashMap<String, Object>() {{
            put("beginTime", tomrrowStr);
            put("status", "state_start");
        }};
        List<PlanVo> patrolPlanList = patrolPlanService.getPlanView(map);
        for (PlanVo planVo : patrolPlanList) {
            if (Objects.equals(planVo.getFrequency(), 0)) {
                log.error(ConstantForDevice.PATRAL_PLAN.equals(planVo.getPlanType()) ? "巡检计划" + planVo.getPlanNo() + "时间间隔不能为0" : "保养计划" + planVo.getPlanNo() + "时间间隔不能为0");
                continue;
            }
            JSONArray array = new JSONArray();
            Map<String, Object> conditionMap = new HashMap<String, Object>();
            conditionMap.put("planId", planVo.getId());
            if (ConstantForDevice.PATRAL_PLAN.equals(planVo.getPlanType())) {
                //TODO 巡检计划生成
                List<PatrolRecordDO> patrolRecordList = patrolRecordService.list(conditionMap);
                if (patrolRecordList.size() > 0) {
                    //获取最新一条
                    PatrolRecordDO patrolRecord = patrolRecordList.get(0);
                    //array = getDoTimes(now, patrolRecord.getStartTime(), planVo.getFrequency(), planVo.getExpireTime(), planVo.getPlanType());
                } else {
                    array = getDoTimes(now, planVo.getEndTime(), planVo.getFrequency(), planVo.getExpireTime(), planVo.getPlanType());
                }
                try {
                    saveRecordByPlan(array, planVo);
                } catch (Exception e) {
                    continue;
                }
            } else if (ConstantForDevice.UPKEEP_PLAN.equals(planVo.getPlanType())) {
                //TODO 保养计划生成
                List<UpkeepRecordDO> upkeepRecordList = upkeepRecordService.list(conditionMap);
                //2、该计划单已生成过保养记录单
                if (upkeepRecordList.size() > 0) {
                    //获取最新一条
                    UpkeepRecordDO upkeepRecordDO = upkeepRecordList.get(0);
                    array = getDoTimes(now, upkeepRecordDO.getEndTime(), planVo.getFrequency(), planVo.getExpireTime(), planVo.getPlanType());
                } else {
                    //3、该计划单未生成过保养记录单
                    array = getDoTimes(now, null, planVo.getFrequency(), planVo.getExpireTime(), planVo.getPlanType());
                }
                try {
                    saveRecordByPlan(array, planVo);
                } catch (Exception e) {
                    continue;
                }
            }
        }
    }

    private synchronized void excuteNotice2() throws ParseException {
        //巡检计划单
        PatrolPlanService patrolPlanService = ApplicationContextRegister.getBean(PatrolPlanService.class);
        PatrolRecordService patrolRecordService = ApplicationContextRegister.getBean(PatrolRecordService.class);
        UpkeepRecordService upkeepRecordService = ApplicationContextRegister.getBean(UpkeepRecordService.class);
        PatrolPlanDao patrolPlanDao = ApplicationContextRegister.getBean(PatrolPlanDao.class);
        UpkeepPlanDao upkeepPlanDao = ApplicationContextRegister.getBean(UpkeepPlanDao.class);
        Date now = new Date();
        Map<String, Object> map = new HashMap<String, Object>() {{
            put("sync", 1);
            put("status", "state_start");
        }};
        List<PlanVo> patrolPlanList = patrolPlanService.getPlanView(map);
        if (ObjectUtils.isEmpty(patrolPlanList)) {
            return;
        }
//        patrolPlanList = patrolPlanList.stream().filter(plan -> !ObjectUtils.isEmpty(plan) && !ObjectUtils.isEmpty(plan.getSync()) && !plan.getSync().equals(1)).collect(Collectors.toList());
//        if (ObjectUtils.isEmpty(patrolPlanList)){
//            return;
//        }
        List<HashMap<String, Object>> patralMaps = new ArrayList<>();
        List<HashMap<String, Object>> unkeepMaps = new ArrayList<>();
        for (PlanVo planVo : patrolPlanList) {
            if (Objects.equals(planVo.getFrequency(), 0)) {
                log.error(ConstantForDevice.PATRAL_PLAN.equals(planVo.getPlanType()) ? "巡检计划" + planVo.getPlanNo() + "时间间隔不能为0" : "保养计划" + planVo.getPlanNo() + "时间间隔不能为0");
                continue;
            }
            JSONArray list = new JSONArray();
            Map<String, Object> conditionMap = new HashMap<String, Object>();
            conditionMap.put("planId", planVo.getId());
            if (ConstantForDevice.PATRAL_PLAN.equals(planVo.getPlanType())) {
                //TODO 巡检计划生成
                HashMap<String, Object> updateMap = new HashMap<>();
                updateMap.put("sync", 1);
                updateMap.put("id", planVo.getId());
                patralMaps.add(updateMap);
                List<PatrolRecordDO> patrolRecordList = patrolRecordService.list(conditionMap);
                if (!ObjectUtils.isEmpty(patrolRecordList)) continue;
                list = getDoTimes2(planVo.getStartTime(), planVo.getEndTime(), planVo.getPlanType(), planVo.getFrequency());
//                try{
                saveRecordByPlan(list, planVo);
//                }catch(Exception e){
//                    continue;
//                }
            } else if (ConstantForDevice.UPKEEP_PLAN.equals(planVo.getPlanType())) {
                //TODO 保养计划生成
                HashMap<String, Object> updateMap = new HashMap<>();
                updateMap.put("sync", 1);
                updateMap.put("id", planVo.getId());
                unkeepMaps.add(updateMap);
                List<UpkeepRecordDO> upkeepRecordList = upkeepRecordService.list(conditionMap);
                if (!ObjectUtils.isEmpty(upkeepRecordList)) continue;
                list = getDoTimes2(planVo.getStartTime(), planVo.getEndTime(), planVo.getPlanType(), planVo.getFrequency());
                try {
                    saveRecordByPlan(list, planVo);
                } catch (Exception e) {
                    continue;
                }
            }
        }
        if (!ObjectUtils.isEmpty(unkeepMaps)) {

            upkeepPlanDao.batchUpdate(unkeepMaps);
        }
        if (!ObjectUtils.isEmpty(patralMaps)) {
            patrolPlanDao.batchUpdate(patralMaps);
        }
    }

    public static void main(String[] args) throws ParseException {
        //计划间隔
        Date doTime = new Date();
        Date offsetDate = new Date();
        Integer frequency = 365;
        if (frequency.equals(1095)){
            offsetDate = DateUtil.offset(doTime, DateField.YEAR, 3);
        }
        if (frequency.equals(730)){
            offsetDate = DateUtil.offset(doTime, DateField.YEAR, 2);
        }
        if (frequency.equals(365)){
            offsetDate = DateUtil.offset(doTime, DateField.YEAR, 1);
        }
        if (frequency.equals(180)){
            offsetDate = DateUtil.offset(doTime, DateField.MONTH, 6);
        }
        if (frequency.equals(90)){
            offsetDate = DateUtil.offset(doTime, DateField.MONTH, 3);
        }
        if (frequency.equals(30)){
            offsetDate = DateUtil.offset(doTime, DateField.MONTH, 1);
        }
        if (frequency.equals(7)){
            offsetDate = DateUtil.offset(doTime, DateField.WEEK_OF_YEAR, 1);
        }
        System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateUtil.beginOfDay(offsetDate)));

    }
    /**
     * 获取执行时间
     *
     * @param nowDate
     * @param lastTime
     * @param frequency
     * @param expireTime
     * @param planType
     * @return
     * @throws ParseException
     */
    private JSONArray getDoTimes(Date nowDate, Date lastTime, Integer frequency, Integer expireTime, String planType) throws ParseException {
        Integer frequency2 = 0;
        expireTime = ObjectUtils.isEmpty(expireTime) ? 0 : expireTime;
        Integer timeInterval = frequency - expireTime;
        JSONArray array = new JSONArray();
        //zero:明天的起始时间，例如nowDate是2023-12-27 00:00:00 ，那么zero就是2023-12-28 00:00:00
        Long tomrrowDate = DateUtils.addDays(nowDate, 1).getTime();
        long zeroT = tomrrowDate / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
        String zeroString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(zeroT);
        Date zero = DateUtils.parseDate(zeroString, "yyyy-MM-dd HH:mm:ss");
        //明天23点59分59秒的毫秒数
        long endT = zeroT + 24 * 60 * 60 * 1000 - 1;
        String endString = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(endT);
        Date end = DateUtils.parseDate(endString, "yyyy-MM-dd HH:mm:ss");
        //获取下一次计划的时间（中间算上了提前消息提醒的时间，所以这个下次计划时间实际上是下次提醒的时间）
        Date nextNoticeDate = lastTime == null ? zero : (DateUtils.addHours(lastTime, timeInterval).compareTo(zero) > 0 ? DateUtils.addHours(lastTime, timeInterval) : zero);
        //Date nextDate = lastTime == null ? zero : (DateUtils.addHours(lastTime, frequency).compareTo(zero) > 0 ? DateUtils.addHours(lastTime, frequency) : zero);
        Date nextDate = lastTime == null ? zero : lastTime;
        if (nextNoticeDate.compareTo(nowDate) > 0 && end.compareTo(nextNoticeDate) > 0) {
            //循环叠加，指导超过第二天的时间
            Date doTime = nextDate;
            Calendar cal = Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            //未结束
            while (end.compareTo(doTime) >= 0) {
                JSONObject nextPlanObj = new JSONObject();
                nextPlanObj.put("beginTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(doTime));
                nextPlanObj.put("planType", planType);



//                if (frequency == 720) {
//                    calendar.setTime(doTime);
//                    calendar.set(Calendar.DAY_OF_MONTH, 1);
//                    calendar.add(Calendar.MONTH, 1);
//                    nextPlanObj.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
//
//                    cal.setTime(doTime);
//                    cal.set(Calendar.DAY_OF_MONTH, 1);
//                    cal.roll(Calendar.DAY_OF_MONTH, -1);
//                    String format = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
//                    int day = Integer.parseInt(format.substring((format.lastIndexOf("-") + 1)));// 得今天是2022年10月13号，所以这个月的最大天数是 31
//                    frequency2 = day * 24;
//                    doTime = DateUtils.addHours(doTime, frequency2);
//                } else {
//                    if (frequency == 168) {
//                        nextPlanObj.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateUtils.addHours(doTime, frequency)));
//                    } else {
//                        calendar.setTime(doTime);
//                        calendar.add(Calendar.DAY_OF_MONTH, 30);
//                        nextPlanObj.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
//                    }
//                    doTime = DateUtils.addHours(doTime, frequency);
//                }


                DateTime offsetDate = new DateTime();
                //计划间隔
                if (frequency.equals(1095)){
                    offsetDate = DateUtil.offset(doTime, DateField.YEAR, 3);
                }
                if (frequency.equals(730)){
                    offsetDate = DateUtil.offset(doTime, DateField.YEAR, 2);
                }
                if (frequency.equals(365)){
                    offsetDate = DateUtil.offset(doTime, DateField.YEAR, 1);
                }
                if (frequency.equals(180)){
                    offsetDate = DateUtil.offset(doTime, DateField.MONTH, 6);
                }
                if (frequency.equals(90)){
                    offsetDate = DateUtil.offset(doTime, DateField.MONTH, 3);
                }
                if (frequency.equals(30)){
                    offsetDate = DateUtil.offset(doTime, DateField.MONTH, 1);
                }
                if (frequency.equals(7)){
                    offsetDate = DateUtil.offset(doTime, DateField.WEEK_OF_YEAR, 1);
                }
                nextPlanObj.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateUtil.beginOfDay(offsetDate)));
                doTime = offsetDate;



                array.add(nextPlanObj);
            }
        }
        return array;
    }


    /**
     * 获取执行时间
     *
     * @param planType
     * @return
     * @throws ParseException
     */
    private JSONArray getDoTimes2(Date startDate, Date endDate, String planType, Integer frequency) throws ParseException {
        Integer frequency2 = 0;
        List<JSONArray> result = new ArrayList<>();
        JSONArray array = new JSONArray();
        DateTime beginDay = DateUtil.beginOfDay(startDate);
        DateTime endDay = DateUtil.beginOfDay(endDate);
        DateTime today = DateUtil.beginOfDay(DateUtil.parse(DateUtil.today()));
        Calendar cal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        while (beginDay.isBefore(endDay) && beginDay.isBefore(today)) {
            JSONObject nextPlanObj = new JSONObject();
            nextPlanObj.put("beginTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(beginDay));
            nextPlanObj.put("planType", planType);
//            if (frequency == 730) {
//                calendar.setTime(beginDay);
//                calendar.set(Calendar.DAY_OF_MONTH, 1);
//                calendar.add(Calendar.MONTH, 1);
//                nextPlanObj.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
//
//                cal.setTime(beginDay);
//                cal.set(Calendar.DAY_OF_MONTH, 1);
//                cal.roll(Calendar.DAY_OF_MONTH, -1);
//                String format = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
//                int day = Integer.parseInt(format.substring((format.lastIndexOf("-") + 1)));// 得今天是2022年10月13号，所以这个月的最大天数是 31
//                frequency2 = day * 24;
//                beginDay = DateUtil.offset(beginDay, DateField.HOUR, frequency2);
//            } else {
//                if (frequency == 180) {
//                    nextPlanObj.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateUtil.offset(beginDay, DateField.DAY_OF_MONTH, 1)));
//                } else {
//                    calendar.setTime(beginDay);
//                    calendar.add(Calendar.DAY_OF_MONTH, 30);
//                    nextPlanObj.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
//                }
//
//                beginDay = DateUtil.offset(beginDay, DateField.HOUR, frequency);
//            }
            DateTime offsetDate = new DateTime();
            log.error("================22222222222222222222222:");
            log.error("================frequency：{}:",frequency);
            //计划间隔
            if (frequency.equals(1095)){
                offsetDate = DateUtil.offset(beginDay, DateField.YEAR, 3);
            }
            if (frequency.equals(730)){
                offsetDate = DateUtil.offset(beginDay, DateField.YEAR, 2);
            }
            if (frequency.equals(365)){
                offsetDate = DateUtil.offset(beginDay, DateField.YEAR, 1);
            }
            if (frequency.equals(180)){
                offsetDate = DateUtil.offset(beginDay, DateField.MONTH, 6);
            }
            if (frequency.equals(90)){
                offsetDate = DateUtil.offset(beginDay, DateField.MONTH, 3);
            }
            if (frequency.equals(30)){
                offsetDate = DateUtil.offset(beginDay, DateField.MONTH, 1);
            }
            if (frequency.equals(7)){
                offsetDate = DateUtil.offset(beginDay, DateField.WEEK_OF_YEAR, 1);
            }
            nextPlanObj.put("endTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(DateUtil.beginOfDay(offsetDate)));
            beginDay = offsetDate;
            array.add(nextPlanObj);
            result.add(array);

        }
        return array;
    }

    /**
     * 调用service层方法，生成巡检任务单和保养任务单
     *
     * @param array
     * @param planVo
     * @throws ParseException
     */
    private void saveRecordByPlan(JSONArray array, PlanVo planVo) throws ParseException {
        PatrolRecordService patrolRecordService = ApplicationContextRegister.getBean(PatrolRecordService.class);
        UpkeepPlanService upkeepPlanService = ApplicationContextRegister.getBean(UpkeepPlanService.class);
        NoticeService noticeService = ApplicationContextRegister.getBean(NoticeService.class);
        String titel = "";
        String content = "";

        Long type = 1L;
        JSONObject contentDetail = new JSONObject();
        contentDetail.put("id", planVo.getId());
        List<Long> toUsers = new ArrayList<>();
        toUsers.add(planVo.getEngineerId());
        if (ConstantForDevice.PATRAL_PLAN.equals(planVo.getPlanType())) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = array.getJSONObject(i);
                Map map = patrolRecordService.addRecordByPlan(planVo.getId(), DateUtils.parseDate(object.get("beginTime").toString(), "yyyy-MM-dd HH:mm:ss"), DateUtils.parseDate(object.get("endTime").toString(), "yyyy-MM-dd HH:mm:ss"));
                Long billId = (Long) map.get("id");
                titel = "定期点检执行信息已生成";
                content = "您好，定期点检" + planVo.getName() + "的定期点检执行信息已生成请及时处理。";
                contentDetail.put("url", "/inspection/daily_handle?id=" + billId);
                type = 400L;
                //发送消息到负责人
                try {
                    noticeService.saveAndSendSocket(titel, content, billId, contentDetail.toString(), type, ShiroUtils.getUserId(), toUsers);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        } else if (ConstantForDevice.UPKEEP_PLAN.equals(planVo.getPlanType())) {
            for (int i = 0; i < array.size(); i++) {
                JSONObject object = array.getJSONObject(i);
                Long billId = upkeepPlanService.makeWorkOrder(planVo.getId(), DateUtils.parseDate(object.get("beginTime").toString(), "yyyy-MM-dd HH:mm:ss"), DateUtils.parseDate(object.get("endTime").toString(), "yyyy-MM-dd HH:mm:ss"));
                titel = "保养执行信息已生成";
                content = "您好，" + planVo.getName() + "的执行信息已经生成";
                contentDetail.put("url", "/upkeep/execution_handle?id=" + billId);
                type = 402L;
                //发送消息到负责人
                try {
                    noticeService.saveAndSendSocket(titel, content, billId, contentDetail.toString(), type, ShiroUtils.getUserId(), toUsers);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

        }


    }

    /**
     * 调用service层方法，生成巡检任务单和保养任务单
     *
     * @param planVo
     * @throws ParseException
     */
    private void saveRecordByPlan(List<JSONArray> list, PlanVo planVo) throws ParseException {
        if (ObjectUtils.isEmpty(list)) {
            return;
        }
        PatrolRecordService patrolRecordService = ApplicationContextRegister.getBean(PatrolRecordService.class);
        UpkeepPlanService upkeepPlanService = ApplicationContextRegister.getBean(UpkeepPlanService.class);
        if (ConstantForDevice.PATRAL_PLAN.equals(planVo.getPlanType())) {
            for (int j = 0; j < list.size(); j++) {
                JSONArray array = list.get(j);
                for (int i = 0; i < array.size(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    patrolRecordService.addRecordByPlan(planVo.getId(), DateUtils.parseDate(object.get("beginTime").toString(), "yyyy-MM-dd HH:mm:ss"), DateUtils.parseDate(object.get("endTime").toString(), "yyyy-MM-dd HH:mm:ss"));
                }
            }
        } else if (ConstantForDevice.UPKEEP_PLAN.equals(planVo.getPlanType())) {
            for (int j = 0; j < list.size(); j++) {
                JSONArray array = list.get(j);
                for (int i = 0; i < array.size(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    upkeepPlanService.makeWorkOrder(planVo.getId(), DateUtils.parseDate(object.get("beginTime").toString(), "yyyy-MM-dd HH:mm:ss"), DateUtils.parseDate(object.get("endTime").toString(), "yyyy-MM-dd HH:mm:ss"));
                }
            }
        }
    }

    /**
     * 更新计划单状态
     */
    private void updatePatrolPlan() {
        //巡检计划单
        PatrolPlanService patrolPlanService = ApplicationContextRegister.getBean(PatrolPlanService.class);
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(1);
        params.put("planEndTime", DateFormatUtil.getFormateDate(new Date()));
        // 129 为启用状态
        params.put("status", ConstantForDevice.STATE_START);
        List<Map<String, Object>> planList = patrolPlanService.planList(params);
        List<Long> planIds = new ArrayList<>();
        if (planList.size() > 0) {
            PatrolPlanDO patrolPlan = null;
            for (Map<String, Object> map : planList) {
                planIds.add(Long.parseLong(map.get("id").toString()));
            }
            if (planIds.size() > 0) {
                for (Long planId : planIds) {
                    patrolPlan = new PatrolPlanDO();
                    patrolPlan.setId(planId);
                    // 131 为已完成
                    patrolPlan.setStatus(ConstantForDevice.STATE_STOP_OVER);
                    patrolPlanService.update(patrolPlan);
                }
            }
        }
    }
}
